﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipSys.DataLayer;
using VipSys.SystemEntity;
using VipSys.Common.Library;

namespace VipSys
{
    public partial class LogMag : System.Web.UI.Page
    {
        private List<LogInfo> m_lstLogInfo;
        protected void Page_Load(object sender, EventArgs e)
        {
            string action = Request["action"];
            if (!string.IsNullOrEmpty(action))
            {
                switch (action)
                {
                    case "BindGrid":
                        BindGrid();
                        return;
                    case "Del":
                        Del();
                        return;
                    default:
                        return;
                }
            }
        }

     
        private void Del()
        {
            string jsons = "";
            var deletesplit = Request["deletesplit"];
            foreach (var ID in deletesplit.Split(','))
            {
                short shRet = LogAccess.Instance.Del(ID);
                if (shRet != SystemData.ReturnValue.OK)
                {
                    jsons = "{\"success\":true,msg:\"删除失败\"}";
                    break;
                }
            }
            if (string.IsNullOrEmpty(jsons))
                jsons = "{\"success\":true,msg:\"删除成功\"}";
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        private void BindGrid()
        {
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            Orgnization query = new Orgnization();
            query.Start = Request.Form["start"];
            query.Limit = Request.Form["limit"];
            string MenuTitle = Request.Form["MenuTitle"];
            if (query.Start != null && query.Limit != null)
            {
                json.success = true;
                short shRet = LogAccess.Instance.GetLogPageList(query.Start, query.Limit, ref this.m_lstLogInfo);

                foreach (LogInfo item in this.m_lstLogInfo)
                {
                    json.AddItem("ID", item.ID.ToString());
                    json.AddItem("Date", item.Date);
                    json.AddItem("Level", item.Level);
                    json.AddItem("Logger", item.Logger);
                    json.AddItem("Message",  item.Message.Replace("\r", "").Replace("\n", " ").Replace("\"", ""));
                    json.ItemOk();
                }
                int totalCount = 0;
                shRet=LogAccess.Instance.GetTotalCount(ref totalCount);
                json.totlalCount = totalCount;
                if (json.totlalCount > 0)
                {
                    jsons = json.ToString();
                }
                else
                {
                    jsons = @"[]";
                }
            }
            else
            {
                jsons = @"{success:false}";
            }

            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
       
    }
}